//
//  MyInviteViewController.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/5/12.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "MyInviteViewController.h"

#import "UserInviteModel.h"

#import "MyInviteCell.h"

@interface MyInviteViewController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UISearchBar *searchBar;

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSMutableArray *sourceDataArr;

@property (nonatomic,assign) NSInteger currentPage;

@property (nonatomic,copy) NSString *searchTelPhone;

@end

@implementation MyInviteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.topTitle = @"我的邀请";
    self.searchTelPhone = @"";
    self.sourceDataArr = [NSMutableArray array];
    
    [self initUI];
    [self getNewDataWithTelPhone:self.searchTelPhone];
}

#pragma mark - data
-(void)getNewDataWithTelPhone:(NSString *)telPhone{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"telphone"] = telPhone;
    params[@"module"] = [NSString stringWithFormat:@"%ld",(long)self.type];
    params[@"type"] = [NSString stringWithFormat:@"%ld",(long)self.sourceType];
    params[@"page"] = @(self.currentPage);
    
    self.sourceDataArr = [NSMutableArray array];
    [NetWorkingPerson getUserRecordListWithParams:params WithSuccessBlock:^(id  _Nonnull responseObj) {
        [self endRefreshing];
        NSDictionary *data = responseObj[@"data"];
        NSArray *records = data[@"records"];
        for (NSDictionary *dic in records) {
            UserInviteModel *model = [UserInviteModel paraWithDict:dic];
            [self.sourceDataArr addObject:model];
        }
        [self.tableView reloadData];
    }];
}
-(void)getMoreDataWithTelPhone:(NSString *)telPhone{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"telphone"] = telPhone;
    params[@"module"] = [NSString stringWithFormat:@"%ld",(long)self.type];
    params[@"type"] = [NSString stringWithFormat:@"%ld",(long)self.sourceType];
    params[@"page"] = @(self.currentPage);
    
    [NetWorkingPerson getUserRecordListWithParams:params WithSuccessBlock:^(id  _Nonnull responseObj) {
        [self endRefreshing];
        NSDictionary *data = responseObj[@"data"];
        NSArray *records = data[@"records"];
        for (NSDictionary *dic in records) {
            UserInviteModel *model = [UserInviteModel paraWithDict:dic];
            [self.sourceDataArr addObject:model];
        }
        [self.tableView reloadData];
    }];
}
-(void)endRefreshing{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}
#pragma mark - UI
-(void)initUI{
    [self.view addSubview:self.searchBar];
    [self.view addSubview:self.tableView];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.currentPage = 1;
        [self getNewDataWithTelPhone:self.searchTelPhone];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.currentPage = self.currentPage + 1;
        [self.tableView.mj_footer beginRefreshing];
        [self getMoreDataWithTelPhone:self.searchTelPhone];
    }];
}

#pragma mark - lazy
- (UISearchBar *)searchBar{
    if (!_searchBar) {
        _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(15, self.topNavView.bottom+15, ScreenWidth-30, 50)];
        _searchBar.placeholder = @"请输入手机号搜索";
        _searchBar.delegate = self;
        _searchBar.returnKeyType = UIReturnKeySearch;
        _searchBar.backgroundColor = MainBackColor;
        _searchBar.layer.cornerRadius = 25;
        _searchBar.layer.masksToBounds = YES;
        [_searchBar.layer setBorderWidth:1];
        [_searchBar.layer setBorderColor:[UIColor whiteColor].CGColor];
    }
    return _searchBar;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.searchBar.bottom+15, ScreenWidth, ScreenHeight-self.searchBar.bottom - 15 - kSafe_Bottom) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.backgroundColor = UIColor.whiteColor;
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.sourceDataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 52;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyInviteCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyInviteCell"];
    if (!cell) {
        cell = [[MyInviteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyInviteCell"];
    }
    cell.model = self.sourceDataArr[indexPath.row];
    return cell;
}

#pragma mark - SEL
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

#pragma mark - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self.view endEditing:YES];
    self.searchTelPhone = searchBar.text;
    [self getNewDataWithTelPhone:self.searchTelPhone];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    self.searchTelPhone = searchBar.text;
    if (searchBar.text.length <= 0) {
        [self getNewDataWithTelPhone:self.searchTelPhone];
    }
}

@end





